import { useRouter } from 'next/router'
import EditTraining from '../../../src/training/components/EditTraining'
import { useTrainingQuery } from '../../../src/gql'
import { AdminPage } from '../../../src/app'

const EditTrainingPage = () => {
  const router = useRouter()
  const { id } = router.query
  const { data, error, loading } = useTrainingQuery({
    variables: { id: typeof id === 'string' ? id : id[0] },
  })

  let content
  if (loading) content = <p>Loading data...</p>
  if (error) content = <p>Error loading data.</p>
  if (data?.training) content = <EditTraining training={data.training} />
  else content = <p>Training {id} not found.</p>

  return <AdminPage>{content}</AdminPage>
}

export default EditTrainingPage
